require "WebsocketMessage"

local environment = { 
  app = renoise.app,
  song = renoise.song,
  midi = renoise.Midi,
  _callback = {},
  _notifiers = {},
  
  pairs = pairs,
  table = table,
  string = string
}

function runCommand(socket, command)
  if(command:find("[$][$]")) then
    environment._callback[#environment._callback + 1] = function(result)
      socket:send(encodeWebSocketMessage(result))
    end
    command = command:gsub("[$][$]", '_callback[' .. #environment._callback .. ']', 1)
  end
 
  local f = loadstring(command)
  if(f) then
    setfenv(f, environment) 
    return pcall(f)
  end
  return false, "Failed to parse"
end
